package com.demo.spring.redisson;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnBean(RedissonProps.class)
public class RedissonConfig {

    @Autowired
    private RedissonProps redissonProps;

    @Bean
    public RedissonClient redissonClient(){
        Config config = new Config();
        if(redissonProps.getSingleServerConfig()!=null){
            BeanUtils.copyProperties(redissonProps.getSingleServerConfig(),config.useSingleServer());
        }else if(redissonProps.getMasterSlaveServersConfig()!=null){
            BeanUtils.copyProperties(redissonProps.getMasterSlaveServersConfig(),config.useMasterSlaveServers());
        }else if(redissonProps.getSentinelServersConfig()!=null){
            BeanUtils.copyProperties(redissonProps.getSentinelServersConfig(),config.useSentinelServers());
        }else if(redissonProps.getClusterServersConfig()!=null){
            BeanUtils.copyProperties(redissonProps.getClusterServersConfig(),config.useClusterServers());
        }else if(redissonProps.getReplicatedServersConfig()!=null){
            BeanUtils.copyProperties(redissonProps.getReplicatedServersConfig(),config.useReplicatedServers());
        }
        BeanUtils.copyProperties(redissonProps,config);
        return Redisson.create(config);
    }
}
